﻿#include "DACommandsOfWorkFlow.h"
#include "DAWorkFlowGraphicsView.h"
#include "DADataWorkFlow.h"
#include "DAAbstractNodeGraphicsItem.h"
#include "DAGraphicsResizeablePixmapItem.h"

////////////////////////////////////////////////////
//===================================================
// using DA namespace -- 禁止在头文件using！！
//===================================================

using namespace DA;

//===================================================
// DACommandWorkFlowSceneAddBackgroundPixmap
//===================================================
DACommandWorkFlowSceneAddBackgroundPixmap::DACommandWorkFlowSceneAddBackgroundPixmap(DAWorkFlowGraphicsScene* scene,
                                                                                     const QPixmap& pixmap,
                                                                                     QUndoCommand* parent)
    : QUndoCommand(parent), _scene(scene), _pixmap(pixmap)
{
    DAGraphicsResizeablePixmapItem* item = _scene->getBackgroundPixmapItem(true);
    _pixmapOld                           = item->getPixmap();
    item->setVisible(false);
}

DACommandWorkFlowSceneAddBackgroundPixmap::DACommandWorkFlowSceneAddBackgroundPixmap(DAWorkFlowGraphicsScene* scene,
                                                                                     QPixmap&& pixmap,
                                                                                     QUndoCommand* parent)
    : QUndoCommand(parent), _scene(scene)
{
    _pixmap                              = std::move(pixmap);
    DAGraphicsResizeablePixmapItem* item = _scene->getBackgroundPixmapItem(true);
    _pixmapOld                           = item->getPixmap();
}

void DACommandWorkFlowSceneAddBackgroundPixmap::redo()
{
    DAGraphicsResizeablePixmapItem* item = _scene->getBackgroundPixmapItem(true);
    item->setPixmap(_pixmap);
    if (!item->isVisible()) {
        item->show();
    }
}

void DACommandWorkFlowSceneAddBackgroundPixmap::undo()
{
    DAGraphicsResizeablePixmapItem* item = _scene->getBackgroundPixmapItem(true);
    item->setPixmap(_pixmapOld);
    if (_pixmapOld.isNull()) {
        item->hide();
    }
}
